package com.ssrs.platform.util;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 文件类型
 *
 * @author ssrs
 */
public class FileTypes {
    public static final int UNKNOWN = 0;
    public static final int IMAGE = 1;
    public static final int VIDEO = 2;
    public static final int AUDIO = 3;
    public static final int COMPRESSED = 4;
    public static final int WORD = 5;
    public static final int EXCEL = 6;
    public static final int PPT = 7;
    public static final int PDF = 8;
    public static final int FLASH = 9;
    public static final int JS = 10;
    public static final int CSS = 11;

    protected static final Object[][] EXT_ARRAY = new Object[][]{
            /* Unknown */
            {},
            /* Image */
            {"gif", "jpg", "png", "bmp", "psd", "ai", "jpeg", "tif", "tiff"},
            /* Video */
            {"avi", "mpg", "flv", "mpeg", "rm", "rmvb", "mov", "wmv", "wmp", "mp4"},
            /* Audio */
            {"mp3", "wma", "wav"},
            /* Compressed */
            {"zip", "rar", "tar", "gz", "z", "iso", "cab", "jar"},
            /* Word */
            {"doc", "docx"},
            /* Excel */
            {"xls", "xlsx"},
            /* PPT */
            {"ppt", "pptx"},
            /* PDF */
            {"pdf"},
            /* Flash */
            {"fla"},
            /* JS */
            {"js"},
            /* CSS */
            {"css"}};

    /**
     * 判断文件后缀是否指定类型
     *
     * @param fileType
     * @param ext
     * @return
     */
    public static boolean isFileType(int fileType, String ext) {
        if (fileType > EXT_ARRAY.length || fileType < 0) {
            return false;
        } else {
            if (fileType == UNKNOWN) {
                if (getFileType(ext) == UNKNOWN) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return ArrayUtil.contains(EXT_ARRAY[fileType], ext);
            }
        }
    }

    /**
     * 根据文件名判断文件类型是否属于制定类型
     *
     * @param fileName
     * @param fileTypes
     * @return
     */
    public static boolean fileTypeIn(String fileName, String... fileTypes) {
        String ext = FileUtil.getSuffix(fileName);
        if (ext == null) {
            ext = "";
        } else {
            ext = ext.toLowerCase();
        }
        for (String type : fileTypes) {
            if (ext.equalsIgnoreCase(type)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 检测并获取文件后缀，如果文件后缀不符合则返回null，否则返回文件后缀(不带点)
     *
     * @param fileName
     * @param fileTypes
     * @return
     */
    public static String checkAndGetExt(String fileName, String... fileTypes) {
        String ext = FileUtil.getSuffix(fileName);
        if (ext == null) {
            ext = "";
        } else {
            ext = ext.toLowerCase();
        }
        for (String type : fileTypes) {
            if (ext.equalsIgnoreCase(type)) {
                return ext;
            }
        }
        return null;
    }

    /**
     * 获取文件类型
     *
     * @param ext
     * @return
     */
    public static int getFileType(String ext) {
        if (StrUtil.isEmpty(ext)) {
            return UNKNOWN;
        }
        ext = ext.toLowerCase();
        for (int i = 1; i < EXT_ARRAY.length; i++) {
            Object[] ary = EXT_ARRAY[i];
            if (ArrayUtil.contains(ary, ext)) {
                return i;
            }
        }
        return UNKNOWN;
    }


}
